<template>
  <div class="my-content-container">
    <!-- 顶部导航栏 -->
    <div class="top-nav">
      <button class="back-btn" @click="goBack">
        <svg width="24" height="24" viewBox="0 0 24 24" fill="none">
          <path d="M15 18L9 12L15 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
        </svg>
      </button>
      <h2 class="nav-title">我的内容</h2>
      <div style="width: 24px;"></div>
    </div>

    <!-- 内容区域 -->
    <div class="content-area">
      <!-- 加载状态 -->
      <div v-if="loading" class="loading-container">
        <div class="loading-spinner"></div>
        <p>加载中...</p>
      </div>

      <!-- 内容列表 -->
      <div v-else-if="contents.length > 0" class="content-list">
        <div 
          v-for="content in contents" 
          :key="content.id"
          class="content-item"
        >
          <!-- 内容头部 -->
          <div class="content-header">
            <h3 class="content-title">{{ content.title }}</h3>
            <span :class="['content-status', content.status === 2 ? 'published' : 'draft']">
              {{ content.status === 2 ? '已发布' : '草稿' }}
            </span>
          </div>

          <!-- 描述 -->
          <p v-if="content.description" class="content-description">{{ content.description }}</p>

          <!-- 标签 -->
          <div v-if="content.tag_names && content.tag_names.length > 0" class="content-tags">
            <span 
              v-for="tag in content.tag_names" 
              :key="tag"
              class="tag"
            >
              {{ tag }}
            </span>
          </div>

          <!-- 统计数据 -->
          <div class="content-stats">
            <div class="stat-item">
              <span class="stat-icon">👁️</span>
              <span class="stat-value">{{ content.view_count }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-icon">❤️</span>
              <span class="stat-value">{{ content.like_count }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-icon">💬</span>
              <span class="stat-value">{{ content.comment_count }}</span>
            </div>
            <div class="stat-item">
              <span class="stat-icon">🗓️</span>
              <span class="stat-value">{{ formatDate(content.created_at) }}</span>
            </div>
          </div>

          <!-- 操作按钮 -->
          <div class="content-actions">
            <button class="action-btn edit" @click="editContent(content.id)">
              编辑
            </button>
            <button class="action-btn delete" @click="deleteContent(content.id)">
              删除
            </button>
          </div>
        </div>

        <!-- 加载更多 -->
        <div v-if="hasMore" class="load-more">
          <button @click="loadMore" class="load-more-btn" :disabled="loadingMore">
            {{ loadingMore ? '加载中...' : '加载更多' }}
          </button>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-else class="empty-state">
        <div class="empty-icon">📝</div>
        <h3>还没有发布任何内容</h3>
        <p>快去发布第一条内容吧！</p>
        <button class="empty-btn" @click="goToPublish">
          去发布
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { contentAPI } from '../api'

const router = useRouter()

// 响应式数据
const contents = ref([])
const loading = ref(false)
const loadingMore = ref(false)
const hasMore = ref(true)
const page = ref(1)
const size = ref(20)

// 加载我的内容
const loadMyContent = async () => {
  try {
    loading.value = true
    const response = await contentAPI.getMyContent({
      page: page.value,
      size: size.value
    })
    
    if (response.data.success) {
      if (page.value === 1) {
        contents.value = response.data.data
      } else {
        contents.value = [...contents.value, ...response.data.data]
      }
      
      hasMore.value = response.data.data.length === size.value
    }
  } catch (error) {
    console.error('加载内容失败:', error)
    alert('加载失败，请稍后重试')
  } finally {
    loading.value = false
    loadingMore.value = false
  }
}

// 加载更多
const loadMore = async () => {
  loadingMore.value = true
  page.value++
  await loadMyContent()
}

// 编辑内容
const editContent = (contentId) => {
  // TODO: 跳转到编辑页面
  alert(`编辑功能开发中，ID: ${contentId}`)
}

// 删除内容
const deleteContent = async (contentId) => {
  if (!confirm('确定要删除这条内容吗？')) {
    return
  }
  
  try {
    // TODO: 调用删除API
    alert(`删除功能开发中，ID: ${contentId}`)
    // 刷新列表
    page.value = 1
    await loadMyContent()
  } catch (error) {
    console.error('删除失败:', error)
    alert('删除失败，请稍后重试')
  }
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'short',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 去发布
const goToPublish = () => {
  router.push('/publish')
}

// 组件挂载时加载数据
onMounted(() => {
  loadMyContent()
})
</script>

<style scoped>
.my-content-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
}

.top-nav {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 56px;
  background: #fff;
  border-bottom: 1px solid #e9ecef;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  z-index: 1000;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.back-btn {
  background: transparent;
  border: none;
  padding: 8px;
  cursor: pointer;
  color: #333;
}

.nav-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.content-area {
  margin-top: 56px;
  padding: 20px;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #999;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f0f0f0;
  border-top-color: #ff2442;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.content-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.content-item {
  background: white;
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.content-header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  margin-bottom: 12px;
}

.content-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.content-status {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.content-status.published {
  background: #4caf50;
  color: white;
}

.content-status.draft {
  background: #ff9800;
  color: white;
}

.content-description {
  color: #666;
  font-size: 14px;
  line-height: 1.6;
  margin-bottom: 12px;
}

.content-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.tag {
  background: #ff2442;
  color: white;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
}

.content-stats {
  display: flex;
  gap: 16px;
  padding: 12px 0;
  border-top: 1px solid #f0f0f0;
  border-bottom: 1px solid #f0f0f0;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  color: #666;
  font-size: 13px;
}

.stat-icon {
  font-size: 16px;
}

.content-actions {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}

.action-btn {
  flex: 1;
  padding: 8px 16px;
  border: 1px solid #e9ecef;
  border-radius: 8px;
  background: white;
  color: #666;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn.edit:hover {
  border-color: #2196f3;
  color: #2196f3;
}

.action-btn.delete:hover {
  border-color: #f44336;
  color: #f44336;
}

.load-more {
  padding: 24px 0;
  text-align: center;
}

.load-more-btn {
  background: #ff2442;
  color: white;
  border: none;
  padding: 12px 32px;
  border-radius: 24px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
}

.load-more-btn:hover:not(:disabled) {
  background: #ff4569;
}

.load-more-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  text-align: center;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-state h3 {
  font-size: 18px;
  color: #333;
  margin-bottom: 8px;
}

.empty-state p {
  color: #999;
  font-size: 14px;
  margin-bottom: 24px;
}

.empty-btn {
  background: #ff2442;
  color: white;
  border: none;
  padding: 12px 32px;
  border-radius: 24px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s;
}

.empty-btn:hover {
  background: #ff4569;
}
</style>

